<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 游戏统计
 *
 * @icon fa fa-circle-o
 */
class Game extends Backend
{


    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\GameRankingInfo;

    }

    public function cxselect(){
        $list = [
                [
                    'value'=>0,
                    'name'=>'全部'
                ],
                [
                    'value'=>1,
                    'name'=>'乒乓游戏'
                ],
            [
                'value'=>2,
                'name'=>'飞机游戏'
            ],
            [
                'value'=>3,
                'name'=>'飞机游戏'
            ],
        ];

        $this->success('', null, $list);
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    //月统计
    public function index()
    {

        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }

        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $model = new \app\admin\model\GameRankingInfo();

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $filter = $_GET["filter"]??"";
            $game_id = 0;
            $start_date = $end_date = '';
            if($filter){
                $filter_ar = json_decode($filter,true);
                if($filter_ar && is_array($filter_ar)){
                    $game_id = (int)($filter_ar['game_id']??0);
                    $time = $filter_ar['time']??"";
                    if($time){
                        $time_ar = explode(' - ',$time);
                        if($time_ar && is_array($time_ar)){
                            $start_date = $time_ar[0]??"";
                            $end_date = $time_ar[1]??"";
                        }
                    }
                }
            }

            $nowTime = time();

            $start_month = $this->request->get('start_month');
            if(empty($start_month)){
                $start_month = date("Y-m",$nowTime - 86400 * 30 * 6);
            }
            if($start_date){
                $start_month = date("Y-m",strtotime($start_date));
            }

            $startTime = strtotime($start_month);
            $end_month = $this->request->get('end_month');
            if(empty($end_month)){
                $end_month = date("Y-m");
            }
            if($end_date){
                $end_month = date("Y-m",strtotime($end_date));
            }
            $time = strtotime($end_month);
            $endTime = strtotime("+1 month",$time);
            $sort = "mon";
            $model
                ->where("unix_timestamp(`created_at`) >= ". $startTime." and unix_timestamp(`created_at`) < ".$endTime)
                ->field("game_id,game_name,count(*) as num,count(DISTINCT player_id) as user_num,sum(coin) as coin,updated_at,from_unixtime(unix_timestamp(created_at), '%Y-%m') as mon")
                ->order($sort, $order)
                ->group("game_id,from_unixtime(unix_timestamp(created_at), '%Y-%m')");
               if($game_id>0){
                   $model->where(["game_id"=>$game_id]);
               }
               // ->buildSql();
            $list = $model->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }

        return $this->view->fetch();
    }


    //日统计
    public function date()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $this->request->filter(['strip_tags', 'trim']);

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $model = new \app\admin\model\GameRankingInfo();
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $filter = $_GET["filter"]??"";
            $game_id = 0;
            $search_start_date = $search_end_date = '';
            if($filter){
                $filter_ar = json_decode($filter,true);
                if($filter_ar && is_array($filter_ar)){
                    $game_id = (int)($filter_ar['game_id']??0);
                    $time = $filter_ar['time']??"";
                    if($time){
                        $time_ar = explode(' - ',$time);
                        if($time_ar && is_array($time_ar)){
                            $search_start_date = $time_ar[0]??"";
                            $search_end_date = $time_ar[1]??"";
                        }
                    }
                }
            }

            $month = $this->request->get('month');
            $c_game_id = (int)$this->request->get('game_id');
            if($month){
                $month_time = strtotime($month);
                $month_time1 =  strtotime("+1 month",$month_time);
                $month_time1 = $month_time1 - 3600;
                $month_start_date = date("Y-m-d",$month_time);
                $month_end_date = date("Y-m-d",$month_time1);
                $search_start_date = $search_start_date?:$month_start_date;
                $search_end_date = $search_end_date?:$month_end_date;
            }
            $game_id = $game_id>0?$game_id:$c_game_id;

            $nowTime = time();
            $start_date = $this->request->get('start_date');
            if(empty($start_date)){
                $start_date = date("Y-m-d",$nowTime - 86400 * 30 * 6);
            }
            if($search_start_date){
                $start_date = date("Y-m-d",strtotime($search_start_date));
            }
            $startTime = strtotime($start_date);

            $end_date = $this->request->get('end_date');
            if(empty($end_date)){
                $end_date = date("Y-m-d");
            }
            if($search_end_date){
                $end_date = date("Y-m-d",strtotime($search_end_date));
            }
            $endTime = strtotime($end_date);
            $endTime = $endTime + 86400;

            $sort = "mon";
            $model
                ->where("unix_timestamp(`created_at`) >= ". $startTime." and unix_timestamp(`created_at`) < ".$endTime)
                ->field("game_id,game_name,count(*) as num,count(DISTINCT player_id) as user_num,sum(coin) as coin,updated_at,from_unixtime(unix_timestamp(created_at), '%Y-%m-%d') as mon")
                ->order($sort, $order)
                ->group("game_id,from_unixtime(unix_timestamp(created_at), '%Y-%m-%d')");
            if($game_id>0){
                $model->where(["game_id"=>$game_id]);
            }
            // ->buildSql();
            $list = $model->paginate($limit);
            $ar = $list->toArray();

         //   p($ar);die;
            $a = array_column($ar['data'],'num');
            $b = array_column($ar['data'],'mon');

            $ar = [];
            foreach ($b as $b_k=>$b_v){
                if(!isset($ar[$b_v])){
                    $ar[$b_v] = $a[$b_k];
                }else{
                    $ar[$b_v] += $a[$b_k];
                }
            }
            $a = array_values($ar);
            $b = array_keys($ar);
            $this->assignconfig('a', $a);
            $this->assignconfig('b', $b);
            $result = array("total" => $list->total(), "rows" => $list->items(),'a'=>$a,'b'=>$b);
            if($this->request->isAjax()){
                return json($result);
            }

        return $this->view->fetch();
    }


    //日统计
    public function hot()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {
        }
        $this->request->filter(['strip_tags', 'trim']);

        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $model = new \app\admin\model\GameRankingInfo();
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $filter = $_GET["filter"]??"";
        $game_id = 0;
        $date = '';
        if($filter){
            $filter_ar = json_decode($filter,true);
            if($filter_ar && is_array($filter_ar)){
                $game_id = (int)($filter_ar['game_id']??0);
                $date = $filter_ar['date']??"";
            }
        }

        $c_date = $this->request->get('date');
        $date = $c_date?:$date;
        $c_game_id = (int)$this->request->get('game_id');
        $game_id = $game_id>0?$game_id:$c_game_id;
        if(empty($date)){
            $date = date("Y-m-d");
        }
        $startTime = strtotime($date);
        $endTime = $startTime + 86400;
        $sort = "mon";
        $model
            ->where("unix_timestamp(`created_at`) >= ". $startTime." and unix_timestamp(`created_at`) < ".$endTime)
            ->field("game_id,game_name,count(*) as num,count(DISTINCT player_id) as user_num,sum(coin) as coin,updated_at,from_unixtime(unix_timestamp(created_at), '%H') as mon")
            ->order($sort, "asc")
            ->group("from_unixtime(unix_timestamp(created_at), '%H')");
        if($game_id>0){
            $model->where(["game_id"=>$game_id]);
        }
        // ->buildSql();
        $list = $model->paginate(30);
        $ar = $list->toArray();

        $a = array_column($ar['data'],'num');
        $b = array_column($ar['data'],'mon');

        $arr = [];
        for($i=0;$i<=23;$i++){
            $arr[$i]=0;
        }

        foreach ($b as $b_k=>$b_v){
            if(isset($arr[$b_v])){
                $arr[$b_v] = $a[$b_k];
            }
        }
        $a = array_values($arr);
        $b = array_keys($arr);

        $this->assignconfig('a', $a);
        $this->assignconfig('b', $b);
        $result = array("total" => $list->total(), "rows" => $list->items(),'a'=>$a,'b'=>$b);
        if($this->request->isAjax()){
            return json($result);
        }

        return $this->view->fetch();
    }

}
